package prison.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shwread.android.qysw10000000.R;

import java.util.List;

import prison.bean.LetterPushBean;

/**
 * Created by lex on 2017/3/8.
 */

public class LetterPushAdapter extends RecyclerView.Adapter<LetterPushAdapter.ViewHolder> implements View.OnClickListener {

    private List<LetterPushBean.MessagesListBean> list = null;
    private Context context;

    public LetterPushAdapter(Context context,List<LetterPushBean.MessagesListBean> list){
        this.context= context;
        this.list = list;
    }

    private OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_letter_push, null);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        LetterPushBean.MessagesListBean messagesListBean = list.get(position);
        holder.tvName.setText(messagesListBean.getPrisonerName());
        holder.tVDate.setText(messagesListBean.getCreatedAt());
        holder.tVDesc.setText(messagesListBean.getMessage());
        holder.itemView.setTag(list.get(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View view) {
        if (onRecyclerViewItemClickListener != null) {
            onRecyclerViewItemClickListener.onItemClick(view, view.getTag());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tVDesc;
        TextView tVDate;

        public ViewHolder(View itemView) {
            super(itemView);
            tVDesc = (TextView) itemView.findViewById(R.id.tVDesc);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            tVDate = (TextView) itemView.findViewById(R.id.tVDate);
        }
    }
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, Object data);
    }
}
